﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RoleServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class RoleServices : IRoleServices
    {
        private readonly RoleRepository RoleRepository;

        public RoleServices()
        {
            RoleRepository = new RoleRepository(new WcfSessionStorage());
        }

        #region IRoleServices Members

        public List<RoleDTO> GetList(int page, out long recordCount, string sortProperty = null, string sortType = "ASC")
        {
            var Roles = new List<Role>();
            var Results = new List<RoleDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Roles = RoleRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Roles = RoleRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = RoleRepository.RecordCount;

            Roles.ForEach(x =>
                              {
                                  var RoleDto = new RoleDTO();
                                  Mapper.Map(x, RoleDto);
                                  Results.Add(RoleDto);
                              }
                );

            return Results;
        }

        public bool Save(RoleDTO RoleDTO)
        {
            bool res;
            Role Role;
            try
            {
                Role = RoleRepository.GetDataOne(r => r.RoleId == RoleDTO.RoleId);
                var user = new User();
                if (Role == null)
                {
                    Role = new Role();
                    RoleDTO.created_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(RoleDTO.created_by, user);
                    //RoleDTO.modified_by = null;
                }
                else
                {
                    RoleDTO.modified_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(RoleDTO.modified_by, user);
                }
                Mapper.Map(RoleDTO, Role);
                RoleRepository.SaveOrUpdate(Role);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(RoleDTO RoleDto)
        {
            bool res;
            try
            {
                var Role = new Role();
                Mapper.Map(RoleDto, Role);
                RoleRepository.Delete(Role);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<RoleDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                               out long recordCount)
        {
            var Roles = new List<Role>();
            var Results = new List<RoleDTO>();
            Roles = RoleRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = RoleRepository.RecordCount;
            Roles.ForEach(x =>
                              {
                                  var RoleDto = new RoleDTO();
                                  Mapper.Map(x, RoleDto);
                                  Results.Add(RoleDto);
                              }
                );

            return Results;
        }

        #endregion
    }
}